﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JYB.SummitExam.BLL;
using JYB.SummitExam.Model;
using Newtonsoft.Json;

using System.Data;
using System.IO;
using System.Data.OleDb;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using JYB.SummitExam.BLL.BLL;

namespace JYB.SummitExam.UI.Controllers
{
    public class AdminController : Controller
    {
        // GET: /Login/
        CollegeBLL bll = new CollegeBLL();
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 初始化登录页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 初始化显示个人信息页面
        /// </summary>
        /// <returns></returns>
        public ActionResult PersonalInformation()
        {
            return View();
        }
        /// <summary>
        /// 初始化首页
        /// </summary>
        /// <returns></returns>
        public ActionResult HomePage()
        {
            return View();
        }
        /// <summary>
        /// 初始化 导入试题页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ImportQuestions()
        {
            ViewBag.College = bll.GetCollegeList<SystemCollege>();
            ViewBag.Course = bll.GetCollegeList<SystemCourse>();
            return View();
        }
        /// <summary>
        /// 初始化查询试题页面
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectTest()
        {
            ViewBag.College = bll.GetCollegeList<SystemCollege>();
            ViewBag.Course = bll.GetCollegeList<SystemCourse>();
            return View();
        }
        /// <summary>
        /// 组卷策略显示
        /// </summary>
        /// <returns>/Admin/TestPaperStrategis</returns>
        public ActionResult TestPaperStrategis()
        {
            return View();
        }
        /// <summary>
        /// 组建试卷（管理员）
        /// </summary>
        /// <returns></returns>
        public ActionResult TestPaper()
        {
            ViewBag.College = bll.GetCollegeList<SystemCollege>();
            ViewBag.Course = bll.GetCollegeList<SystemCourse>();
            return View();
        }
        /// <summary>
        ///试卷查询（管理员）
        /// </summary>
        /// <returns></returns>
        public ActionResult QueryTestPaper()
        {
            ViewBag.College = bll.GetCollegeList<SystemCollege>();
            ViewBag.Course = bll.GetCollegeList<SystemCourse>();
            return View();
        }
        /// <summary>
        ///初始化学生管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Studentmanagement()
        {
            ViewBag.College = bll.GetCollegeList<SystemCollege>();
            ViewBag.Course = bll.GetCollegeList<SystemCourse>();
            return View();
        }
        /// <summary>
        /// 初始化教师管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Teachermanagement()
        {
            ViewBag.College = bll.GetCollegeList<SystemCollege>();
            return View();
        }
        /// <summary>
        /// 初始化学院管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Collagemanagement()
        {
            return View();
        }
        /// <summary>
        /// 根据学院编号获取该学院下的方向信息
        /// </summary>
        /// <returns></returns>
        public string MajorList(int id)
        {
            return JsonConvert.SerializeObject(bll.MajorList<SystemMajor>(id));
        }
        /// <summary>
        /// 上传试题并将其导入相应的题库
        /// </summary>
        /// <returns></returns>
        public ActionResult ExcelUpLoading(HttpPostedFileBase myExcel)
        {
            if (myExcel == null)
            {
                return Content("<script>alert('请选择文件');location.href='/Admin/ImportQuestions'</script>");
            }
            myExcel.SaveAs(Server.MapPath("/File/" + myExcel.FileName));
            string fileurl = "/File/" + myExcel.FileName;
            DataTable dt = this.ImportExcel(fileurl);
            List<QuestionBank> questionbank = new List<QuestionBank>();
            QuestionBankBLL bll = new QuestionBankBLL();
            foreach (DataRow item in dt.Rows)
            {
                QuestionBank qb = new QuestionBank();
                //qb.QuestionStem = item[1].ToString() + item[2].ToString() + item[3].ToString();
                // qb.Option = item[2].ToString();
                // qb.Answer = item[3].ToString();
                if (item[0].ToString().Length > 0)
                {
                    qb.QuestionStem = item[1].ToString() + item[2].ToString() + item[3].ToString();
                }
                qb.CreateTime = DateTime.Now;
                qb.LastWriteTime = DateTime.Now;
                questionbank.Add(qb);
            }
            foreach (QuestionBank item in questionbank)
            {

                bll.ToLeadQuestion(item);
            }
            Session["dt"] = dt;
            return View();
        }


        #region 导入Excel所需要的方法
        public DataTable ImportExcel(string filepath)
        {
            HSSFWorkbook hssfworkbook;
            try
            {
                using (FileStream file = new FileStream(Server.MapPath(filepath), FileMode.Open, FileAccess.Read))
                {
                    hssfworkbook = new HSSFWorkbook(file);
                }
            }
            catch (Exception)
            {

                throw;
            }
            NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0);
            DataTable dt = new DataTable();
            IRow headerrow = sheet.GetRow(0);
            int cellCount = headerrow.LastCellNum;
            int rowcount = sheet.LastRowNum;
            for (int i = headerrow.FirstCellNum; i < cellCount; i++)
            {
                DataColumn column = new DataColumn(headerrow.GetCell(i).StringCellValue);
                dt.Columns.Add(column);
            }
            for (int i = sheet.FirstRowNum + 1; i < rowcount; i++)
            {
                IRow row = sheet.GetRow(i);
                DataRow dataRow = dt.NewRow();
                if (row != null)
                {
                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) != null)
                            dataRow[j] = GetCellValue(row.GetCell(j));
                    }
                }
                dt.Rows.Add(dataRow);
            }
            return dt;
        }
        private static string GetCellValue(ICell cell)
        {
            if (cell == null)
            {
                return string.Empty;
            }
            switch (cell.CellType)
            {
                case CellType.Blank:
                    return string.Empty;

                case CellType.Boolean:
                    return cell.BooleanCellValue.ToString();

                case CellType.Error:
                    return cell.ErrorCellValue.ToString();

                case CellType.Numeric:


                case CellType.Unknown:

                default:
                    return cell.ToString();
                case CellType.String:
                    return cell.StringCellValue;
                case CellType.Formula:
                    try
                    {
                        HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook);
                        e.EvaluateInCell(cell);
                        return cell.ToString();
                    }
                    catch (Exception e)
                    {
                        return cell.NumericCellValue.ToString();
                    }

            }
        }
        #endregion
    }
}
